package com.tanklab.signature.controller;

import com.tanklab.signature.common.util.JwtUtils;
import com.tanklab.signature.ds.req.AddFileReq;
import com.tanklab.signature.ds.resp.CommonResp;
import com.tanklab.signature.service.FilePreviewService;
import com.tanklab.signature.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/signature/file")
@ApiModel
public class FileController {
    @Autowired
    FileService fileService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private FilePreviewService filePreviewService;

//    @ApiOperation(value="【学生】新增文件，填写必要信息")
//    @PostMapping("/addFile")
//    public CommonResp AddFile(@RequestBody AddFileReq addfilereq) {
//        return fileService.addFile(addfilereq);
//    }
//
//    @ApiOperation(value = "查询指定文件的详细信息")
//    @GetMapping("/queryOneFile/{fileId}")
//    public CommonResp queryOneFile(
//            @ApiParam(name = "fileId", value = "文件编号", example = "1", required = true)
//            @PathVariable("fileId") Long fileId
//    ){
//        return fileService.queryOneFile(fileId);
//    }
//
//    @ApiOperation(value = "查询某学生上传文件的所有详细信息")
//    @GetMapping("/queryFileByStudentId/{studentId}")
//    public CommonResp queryFileByStudentId(
//            @ApiParam(name = "studentId", value = "文件编号", example = "1", required = true)
//            @PathVariable("studentId") Long studentId
//    ){
//        return fileService.queryFileByStudentId(studentId);
//    }
//
//    @ApiOperation(value = "查询所有文件的信息")
//    @GetMapping("/queryAllFile")
//    public CommonResp queryAllFile(){
//        return fileService.queryAllFile();
//    }

    // 在现有的FileController中添加新方法

    @ApiOperation(value = "获取文件详细信息（包含审批信息）")
    @GetMapping("/detail/{fileId}")
    public CommonResp getFileDetail(
            @ApiParam(name = "fileId", value = "文件ID", example = "1", required = true)
            @PathVariable("fileId") Long fileId) {
        return fileService.getFileDetail(fileId);
    }

    @ApiOperation(value = "查询所有文件列表")
    @GetMapping("/list")
    public CommonResp queryAllFiles() {
        return fileService.queryAllFiles();
    }

    @ApiOperation(value = "根据上传者openId查询文件列表")
    @GetMapping("/list/uploader/{uploaderOpenid}")
    public CommonResp queryFilesByUploader(
            @ApiParam(name = "uploaderOpenid", value = "上传者openId", required = true)
            @PathVariable("uploaderOpenid") String uploaderOpenid) {
        return fileService.queryFilesByUploader(uploaderOpenid);
    }

    @ApiOperation(value = "查询当前用户上传的文件列表")
    @GetMapping("/myfiles")
    public CommonResp queryMyFiles(HttpServletRequest request) {
        // 从请求头中获取token
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 从token中提取openid
        String openid = jwtUtils.getOpenidFromToken(token);

        // 调用服务查询当前用户的文件
        return fileService.queryFilesByUploader(openid);
    }

    // Deprecated
    @ApiOperation(value = "删除文件及其关联的申请")
    @DeleteMapping("/delete/{fileId}")
    public CommonResp deleteFile(
            @ApiParam(name = "fileId", value = "文件ID", example = "1", required = true)
            @PathVariable("fileId") Long fileId,
            HttpServletRequest request) {
        // 从请求头中获取token
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 从token中提取openid
        String openid = jwtUtils.getOpenidFromToken(token);

        // 调用服务删除文件
        return fileService.deleteFile(fileId, openid);
    }

    @ApiOperation(value = "获取文件预览链接")
    @GetMapping("/preview/{fileId}")
    public CommonResp getFilePreviewUrl(
            @ApiParam(name = "fileId", value = "文件ID", example = "1", required = true)
            @PathVariable("fileId") Long fileId,
            HttpServletRequest request) {

        // 从请求头中获取token
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 从token中提取openid
        String openid = jwtUtils.getOpenidFromToken(token);

        // 生成文件预览链接
        return filePreviewService.generatePreviewUrl(fileId, openid);
    }
}
